package com.hy.utils;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpTool {

	private static Logger logger = LoggerFactory.getLogger(HttpTool.class);
	
	private static String ENCODING = "utf-8";
	
	private static String CONTENT_TYPE = "application/json";
	
	public static String requestGet(String url){
		//logger.info("HttpTool.requestPost 开始 请求url：" + url);
		//创建HttpClient对象
	    CloseableHttpClient client = HttpClients.createDefault();
	    //创建HttpGet对象
	    HttpGet httpGet = new HttpGet(url);
	  //配置请求参数
        RequestConfig requestConfig = RequestConfig.custom()
                .setCookieSpec(CookieSpecs.DEFAULT)
                .setExpectContinueEnabled(true)
                .setSocketTimeout(5000)  
                .setConnectTimeout(5000)  
                .setConnectionRequestTimeout(5000)  
                .build();
        
        httpGet.setConfig(requestConfig);
        
        String respContent = null;
        
        HttpResponse resp;
        try {
        	resp = client.execute(httpGet);
        	if(resp.getStatusLine().getStatusCode() == 200) {
                HttpEntity responseObj = resp.getEntity();
                respContent = EntityUtils.toString(responseObj,"UTF-8");
            }
        } catch (Exception e) {
        	e.printStackTrace();
            //logger.info("HttpTool.requestPost 异常 请求url：" + url + "!");
        }
        //logger.info("HttpTool.requestPost 结束 请求url：" + url + "!");
        //返回数据
        return respContent;
	}
}
